package jscinttools.transmission;

import jscinttools.utilities.aidautilities.AidaFactories;
import jscinttools.utilities.aidautilities.AidaFileReader;
import hep.aida.ITree;
import hep.aida.ITreeFactory;
import hep.aida.ITuple;
import hep.aida.ITupleFactory;
import hep.aida.IDataPointSetFactory;
import hep.aida.IDataPointSet;
import java.lang.System;
import java.lang.String;
import java.lang.Double;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import fr.in2p3.ipnl.aidautilities.TupleReader;
import fr.in2p3.ipnl.utilities.fileutilities.HeaderDecapitator;
import jscinttools.transmission.*;
import jscinttools.utilities.aidautilities.AidaFactories;

/**
 *  Generates the Rates calculation
 *
 *@author     Marc-Antoine Verdier
 *@created    19 mai 2011
 */
public class StabilityLEDAnalysis
{
	/**
	 *  Description of the Method
	 *
	 *@param  path        Description of the Parameter
	 *@param  files       Description of the Parameter
	 *@param  chans       Description of the Parameter
	 *@param  freqHz      Description of the Parameter
	 *@param  bckFiles    Description of the Parameter
	 *@param  bckPath     Description of the Parameter
	 *@param  outPath     Description of the Parameter
	 *@param  splitTerms  Description of the Parameter
	 */
	public void doAnalysisTDC(String[] path, String[] files, String[] bckPath, String[] bckFiles, String outPath, int[] chans, double freqHz, String[] splitTerms)
	{
		try
		{
			try
			{
				//System.out.println("KKKKKKKKKKKKKKKKKKKKKKKKKKKKKK");
				EventLED[] evtLED = new EventLED[files.length];
				for (int i = 0; i < files.length; i++)
				{
					if (path.length > 1)
					{
						HeaderDecapitator hd = new HeaderDecapitator(path[i], files[i], files[i].split(".txt")[0] + "_NoH.txt", 4);
						evtLED[i] = new EventLED(path[i], files[i].split(".txt")[0] + "_NoH.txt");
					}
					else
					{
						HeaderDecapitator hd = new HeaderDecapitator(path[0], files[i], files[i].split(".txt")[0] + "_NoH.txt", 4);
						evtLED[i] = new EventLED(path[0], files[i].split(".txt")[0] + "_NoH.txt");
					}
					evtLED[i].makeAidaTDC(files[i].split(".txt")[0] + "_events");
				}

				EventLED[] bckEvtLED = new EventLED[bckFiles.length];
				for (int i = 0; i < bckFiles.length; i++)
				{
					HeaderDecapitator hd = new HeaderDecapitator(bckPath[i], bckFiles[i], bckFiles[i].split(".txt")[0] + "_NoH.txt", 4);
					bckEvtLED[i] = new EventLED(bckPath[i], bckFiles[i].split(".txt")[0] + "_NoH.txt");
					bckEvtLED[i].makeAidaTDC(bckFiles[i].split(".txt")[0] + "_events");
				}

				ITree tree = AidaFactories.TREE_FACTORY.createTree(outPath + File.separator + "AnalysisStabLED" + ".aida", "xml", ITreeFactory.CREATE);
				TDCRateLED tdcRateLED = new TDCRateLED(evtLED, bckEvtLED, tree);
				tdcRateLED.makeTupleAndDPS(chans, freqHz, splitTerms);
				tree.commit();
				System.out.println("Writting file : " + outPath + File.separator + "AnalysisStabLED");
			} catch (FileNotFoundException fnfe)
			{
				System.out.println(fnfe);
			}
		} catch (IOException ioe)
		{
			System.out.println("Problem with aida file: " + ioe);
		}
	}


	/**
	 *  Description of the Method
	 *
	 *@param  path        Description of the Parameter
	 *@param  files       Description of the Parameter
	 *@param  chans       Description of the Parameter
	 *@param  freqHz      Description of the Parameter
	 *@param  bckFile     Description of the Parameter
	 *@param  splitTerms  Description of the Parameter
	 */
	public void doAnalysisTDC(String path, String[] files, String bckFile, int[] chans, double freqHz, String[] splitTerms)
	{
		doAnalysisTDC(new String[]{path}, files, new String[]{path}, new String[]{bckFile}, path, chans, freqHz, splitTerms);
	}


	/**
	 *  Description of the Method
	 *
	 *@param  path  Description of the Parameter
	 *@param  file  Description of the Parameter
	 */
	public void doAnalysisVI(String path, String file)
	{

	}
}

